﻿using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectPhanHoi()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_PHANHOI
			};
			return View(CreateViewModel(data));
		}


		public ActionResult SelectPhanHoiByIDHopThu(int idhopthu, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDHopThu = idhopthu,
				EnumViewModelType = EnumViewModel.ADMIN_PHANHOI,
				CurrentPage = page
			};
			return View("SelectPhanHoi", CreateViewModel(data));
		}

		public ActionResult SelectPhanHoiByIDHopThuPaging(int idhopthu, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDHopThu = idhopthu,
				EnumViewModelType = EnumViewModel.ADMIN_PHANHOI,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListPhanHoi", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeletePhanHoi(int id)
		{
			return Json(new{
				Success = _repPhanHoi.Delete(id),
				RecordCount = _repPhanHoi.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult DetailOfPhanHoi(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_PHANHOI,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdatePhanHoi(FormCollection forms)
		{
			string id = forms.GetValues("PhanHoi_ID")[0];
			string idhopthu = forms.GetValues("PhanHoi_IDHopThu")[0];
			string noidungphanhoi = forms.GetValues("PhanHoi_NoiDungPhanHoi")[0];

			var phanhoi = _repPhanHoi.SelectByID(int.Parse(id));
			phanhoi.IDHopThu = int.Parse(idhopthu.Replace(".", "").Replace(",", ""));
			phanhoi.NoiDungPhanHoi = noidungphanhoi;

			var result = _repPhanHoi.Save();

			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_PHANHOI,
				IntID = int.Parse(id),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfPhanHoi", CreateViewModel(data));
		}


        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertPhanHoi(FormCollection forms)
        {
            string idhopthu = forms.GetValues("PhanHoi_IDGopY")[0];
            string username = forms.GetValues("PhanHoi_IDNguoiPhanHoi")[0];
            string noidungphanhoi = forms.GetValues("PhanHoi_NoiDungPhanHoi")[0];
            string to = forms.GetValues("PhanHoi_Email")[0];
            string tieude = forms.GetValues("PhanHoi_TieuDe")[0];
            string noidung = forms.GetValues("PhanHoi_NoiDung")[0];

            var emailContent = RenderPartialViewToString("PhanHoiEmailTemplate");
            emailContent = emailContent.Replace("[TieuDe]", tieude);
            emailContent = emailContent.Replace("[NoiDung]", noidung);
            emailContent = emailContent.Replace("[NoiDungPhanHoi]", noidungphanhoi);

            var result = SendingMail("Thư phản hồi từ - " + WebConfiguration.SiteName, emailContent, to);
            if (result)
            {
                var newItem = new PhanHoi
                {
                    IDHopThu = int.Parse(idhopthu),
                    TenNguoiPhanHoi = username,
                    NoiDungPhanHoi = noidungphanhoi,
                    NgayPhanHoi = DateTime.Now
                };
                result = _repPhanHoi.Insert(newItem);
            }
            var data = new DataTransferViewModel
            {
                IDHopThu = int.Parse(idhopthu),
                EnumViewModelType = EnumViewModel.ADMIN_PHANHOI,
                InfoText = result ? "New item has been added" : "Cannot insert new item"
            };
            return View("SelectPhanHoi", CreateViewModel(data));
        }


	}
}